package com.ereport.dao;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.ereport.entity.AccContacts;
import com.ereport.entity.Accounts;
import com.ereport.entity.BudgetPlanner;
import com.ereport.entity.DailyActivity;
import com.ereport.util.ApplicationContext;

public class AccountsDAO {

static Transaction tx;
	
	private  void endOperations(Session session) {
		
		session.close();
		session=null;
	}

	private  Session startOperations()throws Exception {
		
		Session session = ApplicationContext.getHiberSession().openSession();
		tx=session.beginTransaction();
		return session;
		
	}

	public int insert(Accounts ac) throws Exception {
		Session session = startOperations();
		session.saveOrUpdate(ac);
		session.getTransaction().commit();
		endOperations(session);
		return 0;
		
	}

	public List<Accounts> getAccounts(int id) throws Exception {
		List<Accounts> ob=new ArrayList<Accounts>();
		Session session = startOperations();
		String qrystr="From Accounts where exe_id=:id";
		Query qr=session.createQuery(qrystr);
		qr.setInteger("id", id);
		ob=qr.list();
		session.getTransaction().commit();
		endOperations(session);
		return ob;
		
	}
	public List<DailyActivity> getlastcontacted(int id,String org) throws Exception{
		List<DailyActivity> da=new ArrayList<DailyActivity>();
		Session session = startOperations();
		String qrystr="From DailyActivity where exe_id=:id and organisation=:orgname";
		Query qr=session.createQuery(qrystr);
		qr.setInteger("id", id);
		qr.setString("orgname",org);
		da=qr.list();
		session.getTransaction().commit();
		endOperations(session);
		return da;
	}

	public Accounts getAcc(int id, String org) throws Exception {
		Session session = startOperations();
		String qrystr="From Accounts where exe_id=:id and accName=:orgname";
		Query qr=session.createQuery(qrystr);
		qr.setInteger("id", id);
		qr.setString("orgname",org);
		Accounts acc=(Accounts)qr.uniqueResult();
		endOperations(session);
		return acc;
	}

	public List<AccContacts> getCont(int acId) throws Exception {
		List<AccContacts> da=new ArrayList<AccContacts>();
		Session session = startOperations();
		String qrystr="From AccContacts where acc_id=:id";
		Query qr=session.createQuery(qrystr);
		qr.setInteger("id", acId);
		da=qr.list();
		endOperations(session);
		return da;
	}

	public List<Accounts> getAllCli(int id) throws Exception {
		Session session=startOperations();
		String qtr="From Accounts where exe_id=:id";
		Query qry=session.createQuery(qtr);
		qry.setInteger("id",id);
		List<Accounts> qresult=qry.list();
		session.getTransaction().commit();
		endOperations(session);
		return qresult;
	}

	public List<AccContacts> getAccContList(int id, String org) throws Exception {
		Session session=startOperations();
		String qtr="From AccContacts where acc_id=(select id from Accounts where exe_id=:id and accName=:org)";
		Query qry=session.createQuery(qtr);
		qry.setInteger("id",id);
		qry.setString("org", org);
		List<AccContacts> qresult=qry.list();
		endOperations(session);
		return qresult;
	}

	public List<AccContacts> getAccContPers(int id, String org, String div) throws Exception {
		Session session=startOperations();
		String qtr="From AccContacts where acc_id=(select id from Accounts where exe_id=:id and accName=:org) and pin=:div";
		Query qry=session.createQuery(qtr);
		qry.setInteger("id",id);
		qry.setString("org", org);
		qry.setString("div", div);
		List<AccContacts> qresult=qry.list();
		endOperations(session);
		return qresult;
	}
	public AccContacts getAccContPerdet(int id, String org, String div,String contper) throws Exception {
		Session session=startOperations();
		String qtr="From AccContacts where acc_id=(select id from Accounts where exe_id=:id and accName=:org) and pin=:div and contact_name=:cname";
		Query qry=session.createQuery(qtr);
		qry.setInteger("id",id);
		qry.setString("org", org);
		qry.setString("div", div);
		qry.setString("cname", contper);
		AccContacts qresult=(AccContacts) qry.uniqueResult();
		endOperations(session);
		return qresult;
	}

	
}
